const { QueryTypes, Op, where } = require("sequelize");
const Talk = require("../model/talk.model");
const User = require("../model/user.model")
class TalkService {
  async addService(params) {
    const res = await Talk.create(params);
    console.log("--add-talk--service", res);
    return res; 
  }
  async getListService(paramsObj) {
    if (!paramsObj) {
      const res = await Talk.findAll({
        where: {
          istop: 1,
        },
      });
      for(let i =0;i<res.length;i++) {
        const userInfo = await User.findOne({where:{id:res[i].dataValues.userId}});
        console.log(userInfo,"INFO")
        res[i].dataValues['userInfo'] = userInfo.dataValues;
      }
      console.log("--talk--service", res);
      return res;
    } else {
      let istop = 1;
      if(paramsObj&&paramsObj.forumId) istop = 0; 
      console.log("请求list",paramsObj);
      const res = await Talk.findAll({
        where: {
          istop,
          ...paramsObj,
        },
      });
      console.log(res,"?")
      for(let i =0;i<res.length;i++) {
        console.log(res[i].dataValues,"data")
        const userInfo = await User.findOne({where:{id:res[i].dataValues.userId}});
        console.log(userInfo,"INFO")
        res[i].dataValues['userInfo'] = userInfo.dataValues;
      }
      console.log("--talk--service", res.dataValues);
      return res;
    }
  }
}

module.exports = new TalkService();
